package com.example.fwcz.controller;

import com.example.fwcz.entity.*;
import com.example.fwcz.model.vo.Result;
import com.example.fwcz.model.dto.LoginInfo;
import com.example.fwcz.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


@RestController
public class LoginController {
    @Autowired
    UserService userService;
    @PostMapping("/login")
    public Result login(@RequestParam String username,
                        @RequestParam String password){

//        user user=userService.findUserByUsernameAndPassword(loginInfo.getUsername(), loginInfo.getPassword());
        user user=userService.findUserByUsernameAndPassword(username,password);
        // --- test user
        System.out.println("-------------------------------------------");
        System.out.println(username);
        System.out.println(password);
        System.out.println("test user in LoginController"+user);
        System.out.println("-------------------------------------------");

        if(!"ROLE_admin".equals(user.getRole())){
            return Result.create(403,"无权限");
        }
        return Result.ok("登录成功");
    }

    /**
     * 注册用户
     * @param user
     * @return
     */
    @PostMapping("/useradd")
    public Result addUser(@RequestBody user user){
        userService.addUser(user);
        return Result.ok("用户添加成功");
    }

    /**
     * 修改用户信息
     * @param user
     * @return
     */
    @PutMapping("/userchange")
    public Result changeUser(@RequestBody user user){
        userService.changeUser(user);
        return Result.ok("用户信息修改成功");
    }

}
